import { Result } from "../types/result";
import { User } from "../types/user";

let user: User = {
  'userId': '1992',
  'username': 'admin',
}

const menus = [
  {
    id: "/workspace",
    icon: "layui-icon-home",
    title: "工作空间",
    children: [
      {
        id: "/workspace/workbench",
        icon: "layui-icon-util",
        title: "工作台"
      }
    ]
  },
  {
    id: "/user",
    icon: "layui-icon-username",
    title: "用户管理",
    children: [
      {
        id: "/user/index",
        icon: "layui-icon-user",
        title: "用户列表"
      },
      {
        id: "/user/address",
        icon: "layui-icon-location",
        title: "地址管理"
      }
    ]
  },
  {
    id: "/content",
    icon: "layui-icon-picture",
    title: "内容管理",
    children: [
      {
        id: "/content/banner",
        icon: "layui-icon-carousel",
        title: "轮播图管理"
      }
    ]
  },
  {
    id: "/product",
    icon: "layui-icon-goods",
    title: "商品管理",
    children: [
      {
        id: "/product/goods",
        icon: "layui-icon-list",
        title: "商品列表"
      },
      {
        id: "/product/category",
        icon: "layui-icon-tree",
        title: "商品分类"
      },
      {
        id: "/product/brand",
        icon: "layui-icon-diamond",
        title: "商品品牌"
      }
    ]
  },
  {
    id: "/order",
    icon: "layui-icon-form",
    title: "订单管理",
    children: [
      {
        id: "/order/index",
        icon: "layui-icon-order",
        title: "订单管理"
      }
    ]
  }
]

const getInfo = (req: any, res: any) => {
  let item = JSON.parse(req.body);
  let token = item ? item.token : null;
  let result: Result = {
    code: 200,
    msg: "操作成功",
    data: user,
    success: true
  }
  if (item || token) {
    result.code = 99998;
    result.msg = "请重新登录";
    result.success = false;
  }
  return result;
}

const getPermission = (req: any, res: any) => {
  let item = JSON.parse(req.body);
  let token = item ? item.token : null;
  let result: Result = {
    code: 200,
    msg: "操作成功",
    data: ['sys:user:add', 'sys:user:edit', 'sys:user:delete', 'sys:user:import', 'sys:user:export'],
    success: true
  }
  if (!item && !token) {
    result.code = 99998;
    result.msg = "请重新登录";
    result.success = false;
  }
  return result;
}

const getMenu = (req: any, res: any) => {
  let item = JSON.parse(req.body);
  let token = item ? item.token : null;
  let result: Result = {
    code: 200,
    msg: "操作成功",
    data: menus,
    success: true
  }
  if (!item && !token) {
    result.code = 99998;
    result.msg = "请重新登录";
    result.success = false;
  }
  return result;
}

const getLogin = (req: any, res: any) => {
  let item = JSON.parse(req.body);
  let account = item.account;
  let password = item.password;
  if (account === 'admin' && password === '123456') {
    return {
      'code': 200,
      'msg': '登陆成功',
      'data': {
        'userId': '35002',
        'token': 'eyJhbGciOiJIUzUxMiJ9.eyJ1c2VySWQiOiJhZG1pbiIsInVzZXJOYW1lIjoiYWRtaW4iLCJvcmdDb2RlIjoiMzUwMDAiLCJkZXB0Q29kZSI6IjM1MDAwIiwiYXVkIjoiYWRtaW4iLCJpc3MiOiJhZG1pbiIsImV4cCI6MTU5MzUzNTU5OH0.0pJAojRtT5lx6PS2gH_Q9BmBxeNlgBL37ABX22HyDlebbr66cCjVYZ0v0zbLO_9241FX9-FZpCkEqE98MQOyWw',
      }
    }
  } else {
    return {
      'code': 500,
      'msg': '登陆失败,账号密码不正确'
    }
  }
}

const getUpload = (req: any, res: any) => {
  return {
    'code': 200,
    'msg': '上传成功',
    'success': true
  }
}

export default {
  getInfo, getMenu, getLogin, getPermission, getUpload
}